package edu.ccut.saturn.osgi;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import edu.ccut.saturn.component.DatabaseManager;
import edu.ccut.saturn.component.ILogger;
import edu.ccut.saturn.component.SaturnComponentException;
import edu.ccut.saturn.component.SaturnLoggerManager;

public class OSGIBundleLoaderUtil {

	private static final String SQL_DELETE_BUNDLEINFO = "delete from bundlesInfo where bundleFileName = ?";

	private static final String SQL_QUERY_BUNDLESINFO_BY_STATUS = " select bundleFilePath , bundleFileName from bundlesInfo where status = ? ";

	protected static ILogger logger = SaturnLoggerManager.getInstance()
			.getSaturnLogger(SaturnLoggerManager.LOGGER_KEY_LOAD_BUNDLE);
    /**
     * 删除bundle信息
     * @param bundleName
     * @throws SaturnComponentException
     */
	public static void deleteBundlefromDB(String bundleName)
			throws SaturnComponentException {

		Connection connection = null;
		PreparedStatement preparedStatement = null;
		bundleName = bundleName.substring(bundleName.lastIndexOf("\\") + 1);

		try {
			connection = DatabaseManager.getInstance().getConnection();
			if (connection != null) {
				preparedStatement = connection
						.prepareStatement(SQL_DELETE_BUNDLEINFO);

				preparedStatement.setString(1, bundleName);
				preparedStatement.executeUpdate();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (connection != null) {
					connection.close();
				}
				if (preparedStatement != null) {
					preparedStatement.close();
				}
			} catch (SQLException e) {
				throw new SaturnComponentException(e);
			}
		}
	}

    /**
     * 查询无效的bundle列表
     * @return
     * @throws SaturnComponentException
     */
	public static List<String> queryBundleUnused()
			throws SaturnComponentException {

		List<String> nameList = new ArrayList<String>();

		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		Connection connection = null;

		try {
			connection = DatabaseManager.getInstance().getConnection();
			if (connection != null) {
				preparedStatement = connection
						.prepareStatement(SQL_QUERY_BUNDLESINFO_BY_STATUS);

				preparedStatement.setString(1, "disable");

				resultSet = preparedStatement.executeQuery();
				while (resultSet.next()) {
					String bundleFileName = resultSet
							.getString("bundleFileName");
					String bundleFilePath = resultSet
							.getString("bundleFilePath");

					nameList.add(bundleFilePath + "\\" + bundleFileName);
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (resultSet != null) {
					resultSet.close();
				}
				if (preparedStatement != null) {
					preparedStatement.close();
				}
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		return nameList;
	}

}
